package com.orange.controller.oneSay;

import com.orange.bean.oneSay.vo.OneSayReq;
import com.orange.bean.oneSay.vo.OneSayRes;
import com.orange.common.annotation.NotRepeatSubmit;
import com.orange.common.annotation.PassToken;
import com.orange.common.base.controller.impl.BaseControllerImpl;
import com.orange.framework.result.R;
import com.orange.project.oneSay.service.OneSayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/9/8 17:50
 */
@Validated
@RestController
@RequestMapping("/onesay/")
public class OneSayController extends BaseControllerImpl {

    @Autowired
    private OneSayService oneSayService;

    /**
     * 从文件初始化一言数据到数据库
     *
     * @return
     */
    @GetMapping(value = "initOneSayForFile")
    @PassToken
    public R initOneSay(@RequestParam("filePath") @NotNull(message = "文件路径不能为空") String filePath) {
        oneSayService.getOneSayList(filePath);
        return R.ok();
    }

    /**
     * 从缓存获取一条随机的一言数据
     *
     * @return
     */
    @GetMapping(value = "getRandomOneSay")
    @PassToken
    public R<OneSayRes> getRandomOneSay() {
        OneSayRes randomOneSay = oneSayService.getRandomOneSay();
        if (randomOneSay == null) {
            return R.error().message("缓存中无任何数据");
        }
        return R.ok().data(randomOneSay);
    }

    /**
     * 提交一条一言数据
     *
     * @param oneSayReq
     * @return
     */
    @NotRepeatSubmit
    @PassToken
    @PostMapping(value = "commitOneSay")
    public R commitOneSay(@Validated @RequestBody OneSayReq oneSayReq) {
        oneSayService.saveCommitOneSay(oneSayReq);
        return R.ok();
    }

    /**
     * 批量提交一言数据
     *
     * @param oneSayListReq
     * @return
     */
    @NotRepeatSubmit
    @PassToken
    @PostMapping(value = "batchCommitOneSay")
    public R batchCommitOneSay(@RequestBody @Valid List<@Valid OneSayReq> oneSayListReq) {
        oneSayService.batchCommitOneSay(oneSayListReq);
        return R.ok();
    }

}
